layui.use(['layer', 'form', 'admin', 'laydate', 'ax', 'formSelects','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    //普通图片上传
    var uploadInst = upload.render({
    	elem: '#test1'
            ,url: 'https://www.idjglobalapp.com/app-servce/v1/api/upload' //改成您自己的上传接口
            ,method: 'POST'
            ,done: function(res){
                //上传完毕
                if(res.code == 1){
                	layer.msg('上传成功') 
                	$('#demo1').attr('src', res.data);
                }
             }
    });
    
    $(document).on('click', '#addFile', function(data) {
    	layer.open({
    		id:'addCont',
    		title:'添加内容',
    		type:2,
    		area:['80%','80%'],
    		offset: 't',
    		content:Feng.ctxPath + "/modular/information/addfiles"
    	});
    });
    
    form.on('select(statuss)', function(data){
    	var Obj = new Object();　
    	var type = $("#type").val();
    	var url = $("#url").val();
    	var contex = $("#contex").val();
    	Obj.type = type;
    	Obj.url = url;
    	Obj.contex = contex;
    	var str = '<div class="layui-form-item layui-row" style="padding-left: 60px;">';
    	str +='<textarea id="types" name="types" style="display: none;" class="layui-textarea">'+JSON.stringify(Obj)+'</textarea>';
    	if(type == '图片'){
    		str +='<img style="width: 40%;height: 180px;" src="'+url+'" class="layui-upload-img">';
    	}else{
    		str +='<video width="420px" height="180px" controls="controls" autobuffer="autobuffer"  loop="loop" src="'+url+'" style="width:40%;height:180px;"></video>';
    	}
    	str +='<textarea id="substance" style="width: 40%;" id="writeReason" class="layui-textarea">'+contex+'</textarea>';
    	str +='</div>';
    	$("#divfile").append(str);
    });
    
    // 表单提交事件
    form.on('submit(btnSubmits)', function (data) {
    	var comdata = [];
    	$('textarea[name="types"]').each(function (){
    		comdata.push(JSON.parse($(this).val()));
    	});
    	var pic =$("#demo1").attr("src");
        var informationTitle = $("#informationTitle").val();
        var data = {
        	content: JSON.stringify(comdata),
            pic: pic,
            informationTitle: informationTitle,
            status:0
        }
        // 异步请求
        $.ajax({
            url: Feng.ctxPath + "/modular/information/insert",
            data: JSON.stringify(data),
            type: "post",
            contentType: "application/json;charset=UTF-8",
            async: false,//关闭异步请求
            dataType: "json",
            success: function (data) {
                if (data.code == 200) {
                    Feng.success("操作成功！");
                    //传给上个页面，刷新table用
                    // window.parent.location.reload();
                    admin.putTempData('formOk', true);
                    //关掉对话框
                    admin.closeThisDialog();
                } else {
                    Feng.success("操作失败！" + data.message);
                }
            }
            ,
            error: function (data) {
                Feng.error("添加失败！" + data.message)
            }
        });
    });
    
    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
    	var comdata = [];
    	$('textarea[name="types"]').each(function (){
    		comdata.push(JSON.parse($(this).val()));
    	});
    	var pic =$("#demo1").attr("src");
    	var informationTitle = $("#informationTitle").val();
    	var data = {
    			content: JSON.stringify(comdata),
    			pic: pic,
    			informationTitle: informationTitle,
    			status:1
    	}
    	// 异步请求
    	$.ajax({
    		url: Feng.ctxPath + "/modular/information/insert",
    		data: JSON.stringify(data),
    		type: "post",
    		contentType: "application/json;charset=UTF-8",
    		async: false,//关闭异步请求
    		dataType: "json",
    		success: function (data) {
    			if (data.code == 200) {
    				Feng.success("操作成功！");
    				//传给上个页面，刷新table用
    				// window.parent.location.reload();
    				admin.putTempData('formOk', true);
    				//关掉对话框
    				admin.closeThisDialog();
    			} else {
    				Feng.success("操作失败！" + data.message);
    			}
    		}
    	,
    	error: function (data) {
    		Feng.error("添加失败！" + data.message)
    	}
    	});
    });
    
});
